package com.zrrd.jt_admin.set.express.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zrrd.jt_admin.common.bo.AreaBo;
import com.zrrd.jt_admin.common.bo.CityBo;
import com.zrrd.jt_admin.common.bo.ExpressBo;
import com.zrrd.jt_admin.common.bo.ProvinceBo;
import com.zrrd.jt_admin.common.entity.*;
import com.zrrd.jt_admin.common.vo.ExpressPageVo;
import com.zrrd.jt_admin.set.express.mapper.ExpressMapper;
import com.zrrd.jt_admin.set.express.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Zhangyk
 * @since 2024-07-02
 */
@Service
public class ExpressServiceImpl extends ServiceImpl<ExpressMapper, Express> implements ExpressService {

    @Resource
    private ExpressValueService expressValueService;

    @Resource
    private SysAreaService sysAreaService;

    @Resource
    private SysProvinceService sysProvinceService;

    @Resource
    private SysCityService sysCityService;

    @Resource
    private SysDistrictService sysDistrictService;

    @Override
    public ExpressPageVo findByPage(Integer pageNo, Integer pageSize) {
        ExpressPageVo expressPageVo = new ExpressPageVo();
        PageHelper.startPage(pageNo, pageSize);

        //---------------获取List(expressBos)-----------------------
        List<Express> expressList = list();
        PageInfo<Express> pageInfo = new PageInfo<>(expressList);
        List<ExpressBo> expressBos = expressList.stream().map(express -> {
            ExpressBo expressBo = new ExpressBo();
            BeanUtils.copyProperties(express, expressBo);
            List<ExpressValue> expressValues = expressValueService.lambdaQuery()
                    .eq(ExpressValue::getExpressId, express.getId())
                    .list();
            expressBo.setExpressValues(expressValues);
            return expressBo;
        }).collect(Collectors.toList());
        expressPageVo.setList(expressBos);
        //--------------------------------------------------------------------------------

        //-------------------------------------获取List<AreaBo> areas---------------------------

        List<AreaBo> areaBos = sysAreaService.list().stream().map(sysArea -> {
            AreaBo areaBo = new AreaBo();
            BeanUtils.copyProperties(sysArea, areaBo);
            List<ProvinceBo> provinceBos = sysProvinceService.lambdaQuery()
                    .eq(SysProvince::getSysAreaId, sysArea.getId())
                    .list().stream().map(sysProvince -> {
                        ProvinceBo provinceBo = new ProvinceBo();
                        BeanUtils.copyProperties(sysProvince, provinceBo);
                        List<CityBo> cityBos = sysCityService.lambdaQuery()
                                .eq(SysCity::getSysProvinceId, sysProvince.getId())
                                .list().stream().map(sysCity -> {
                                    CityBo cityBo = new CityBo();
                                    BeanUtils.copyProperties(sysCity, cityBo);
                                    List<SysDistrict> sysDistricts = sysDistrictService.lambdaQuery()
                                            .eq(SysDistrict::getSysCityId, sysCity.getId())
                                            .list();
                                    cityBo.setDistricts(sysDistricts);
                                    return cityBo;
                                }).collect(Collectors.toList());
                        provinceBo.setCities(cityBos);
                        return provinceBo;
                    }).collect(Collectors.toList());

            areaBo.setProvinces(provinceBos);
            return areaBo;
        }).collect(Collectors.toList());

        expressPageVo.setAreas(areaBos);
        expressPageVo.setTotalCount(pageInfo.getTotal());
        return expressPageVo;
    }
}
